home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0036_Scalable HEX Screen.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  3KB  |  123 lines

  1. {
  2. VINCE LAURENT
  3.  
  4. I wrote some code to draw a scalable hex field on the screen. Can
  5. anyone give me a hand in optimizing it? There is a lot of redundant
  6. line drawing and positioning... I would also like to be able to have
  7. a fexible amount of hexigons showing.  For example, if the scale is,
  8. say 40, show 19 hexs, if it is smaller, show more (like as many that
  9. could have fit in the area occupied by 19).
  10.  
  11. BTW, this code can be freely used and distributed or completely ignored :-) }
  12.  
  13. Program HexzOnScreen;
  14. Uses
  15.   Graph, Crt;
  16. Type
  17.   PtArray = Array [1..6, 1..2] of Real;
  18. Var
  19.   s1, s2,
  20.   side,
  21.   i, j,
  22.   Gd, Gm  : Integer;
  23.   Pts     : PtArray;
  24.   ErrCode : Integer;
  25.   Sqrt3,
  26.   sts     : Real;
  27.  
  28. begin
  29.   Sqrt3 := Sqrt(3);
  30.   Side  := 40;             { initial hex side length ( min = 8 ) }
  31.   sts   := Side * Sqrt3;
  32.   s1    := 200;
  33.   s2    := 60;     { starting point For hex field }
  34.   InitGraph(Gd, Gm, 'e:\bp\bgi\');
  35.   ErrCode := GraphResult;
  36.   if not ErrCode = grOk then
  37.   begin
  38.     Writeln('Error: ', GraphErrorMsg(ErrCode));
  39.     Halt(0);
  40.   end;
  41.   SetColor(LightGray);
  42.   Delay(10);   { give the screen a chance to toggle to Graph mode }
  43.   For j := 1 to 17 DO
  44.   begin
  45.     Pts[1, 1] := s1;
  46.     Pts[1, 2] := s2;
  47.     Pts[2, 1] := Pts[1, 1] - side;
  48.     Pts[2, 2] := Pts[1, 2];
  49.     Pts[3, 1] := Pts[1, 1] - side - (side / 2);
  50.     Pts[3, 2] := Pts[1, 2] + (sts / 2);
  51.     Pts[4, 1] := Pts[1, 1] - side;
  52.     Pts[4, 2] := Pts[1, 2] + sts ;
  53.     Pts[5, 1] := Pts[1, 1];
  54.     Pts[5, 2] := Pts[4, 2];
  55.     Pts[6, 1] := Pts[1, 1] + (side / 2);
  56.     Pts[6, 2] := Pts[1, 2] + (sts  / 2);
  57.     For I := 1 to 6 DO
  58.     begin
  59.       if i <> 6 then
  60.         Line(Round(Pts[i, 1]),  Round(Pts[i, 2]),
  61.              Round(Pts[i + 1, 1]), Round(Pts[i + 1, 2]))
  62.       else
  63.         Line(Round(Pts[i, 1]), Round(Pts[i, 2]),
  64.              Round(Pts[1, 1]), Round(Pts[1, 2]));
  65.     end;
  66.     Case j OF
  67.       1..2 :
  68.       begin
  69.         s1 := Round(Pts[6, 1] + side);
  70.         s2 := Round(Pts[6, 2]);
  71.       end;
  72.       3..4 :
  73.       begin
  74.         s1 := Round(Pts[5, 1]);
  75.         s2 := Round(Pts[5, 2]);
  76.       end;
  77.       5..6 :
  78.       begin
  79.         s1 := Round(Pts[3, 1]);
  80.         s2 := Round(Pts[3, 2]);
  81.       end;
  82.       7..8 :
  83.       begin
  84.         s1 := Round(Pts[3, 1]);
  85.         s2 := Round(Pts[3, 2] - sts);
  86.       end;
  87.       9..10 :
  88.       begin
  89.         s1 := Round(Pts[1, 1]);
  90.         s2 := Round(Pts[1, 2] - sts);
  91.       end;
  92.       11 :
  93.       begin
  94.         s1 := Round(Pts[6, 1] + side);
  95.         s2 := Round(Pts[6, 2] - sts);
  96.       end;
  97.       12..13 :
  98.       begin
  99.         s1 := Round(Pts[6, 1] + side);
  100.         s2 := Round(Pts[6, 2]);
  101.       end;
  102.       14 :
  103.       begin
  104.         s1 := Round(Pts[5, 1]);
  105.         s2 := Round(Pts[5, 2]);
  106.       end;
  107.       15 :
  108.       begin
  109.         s1 := Round(Pts[3, 1]);
  110.         s2 := Round(Pts[3, 2]);
  111.       end;
  112.       16 :
  113.       begin
  114.         s1 := Round(Pts[3, 1]);
  115.         s2 := Round(Pts[3, 2] - sts);
  116.       end;
  117.     end;
  118.   end;
  119.   Line(s1, s2, Round(s1 + (side / 2)), Round(s2 - sts / 2));
  120.   Readln;
  121.   CloseGraph;
  122. end.
  123.